<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class="handle-box flex flex-center">
                <el-button type="primary" icon="el-icon-folder" @click="submitForm('ruleForm')">保存</el-button>
            </div>

            <div class="table-form-box">
                <h3 class="h3">分类属性</h3>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                    <table border="0" width="100%" cellpadding="0" cellspacing="1">
                        <colgroup>
                            <col width="15%"/>
                            <col width="35%"/>
                            <col width="15%"/>
                            <col width="35%"/>
                        </colgroup>
                        <tbody>
                            <tr class="tr-title">
                                <td colspan="8">基本信息</td>
                            </tr>
                            <tr>
                                <td align="right"><span class="color-ff2200">*</span>名称:</td>
                                <td>
                                    <el-form-item prop="itemName">
                                        <el-input v-model="ruleForm.itemName" size="small"></el-input>
                                    </el-form-item>
                                </td>
                                <td align="right">描述:</td>
                                <td> 
                                    <el-form-item prop="description">
                                        <el-input
                                            v-model="ruleForm.description"
                                            size="small"
                                     
                                        ></el-input>
                                    </el-form-item>
                                </td>
                                
                            </tr>
                        </tbody>
                    </table>
                </el-form>
            </div>

            <!-- 上传附件 -->
            <!-- <upload-files :files="fileList" @fun="dealFiles" v-if="flag"></upload-files> -->
        </div>
    </div>
</template>

<script>
import appMain from '../../../utils/app_main';
import bus from '../../../components/common/bus'

export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            ruleForm: {
                itemName: '',
                description: '',
            },
            currentItemId: '',
            rules: {
                itemName:[{ required: true, message: '请输入名称', trigger: 'blur' }],
            },

            isUpdate: false, //是否是更新
        };
    },
    methods: {

        submitForm () {
            let self = this;
            this.$refs["ruleForm"].validate(valid => {
                if (valid) {
                    let method = '';
                    let params = _.cloneDeep(this.ruleForm);
                    
                    if (this.isUpdate) {
                        method = "dictItemUpdateItemName";  
                    } else {
                        method = "dictItemInsert"
                        params.dictTypeId = this.currentItemId
                    }
                    appMain[method](params)
                        .then(res => {
                            if (res.data.code === 20000) {
                                self.resetForm('ruleForm');
                                self.$message.success(res.data.message);
                                bus.$emit('close_current_tags')
                            } else {
                                self.$message.error(res.data.message);
                            }
                        })
                        .catch(err => {
                            console.log(err);
                        });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            }); 
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        // 上传文件返回
        dealFiles(data) {
            this.fileList = data.backData;
            this.ruleForm.uploadReturns = this.fileList;
        }
    },

    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    created() {
        this.currentItemId = this.$route.query.id;
        if (this.$route.query.editData) {
            let editData = this.$route.query.editData;
            //是更新操作
            this.ruleForm = JSON.parse(editData)
            this.isUpdate = true;
        } else {
            //新增操作
            this.isUpdate = false;
        }
    }
};
</script>

<style  scoped>
.container {
    padding-bottom: 50px;
}
.handle-box {
    padding: 5px 50px;
    width: 100%;
    background-color: #fff;
    position: absolute;
    left: 0;
    bottom: 0px;
    border-bottom: 1px solid #ddd;
    border-top: 1px solid #ddd;
    z-index: 1;
}

.table-form-box table {
    background-color: #666;
}
.table-form-box .h3 {
    font-size: 22px;
    text-align: center;
    font-weight: normal;
    padding: 20px 0;
}
.table-form-box table .tr-title td {
    font-size: 14px;
    line-height: 40px;
}
.table-form-box table td,
.table-form-box table th {
    background-color: #fff;
    padding: 4px;
    min-height: 40px;
    font-size: 12px;
}
.color-ff2200 {
    color: #ff2200;
}
</style>